package apk.yoursms.settings;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import apk.yoursms.R;
import apk.yoursms.YourSmsActivity;

public class Settings extends PreferenceActivity {

    private Menu myMenu;

    static final int IDD_CONFIRM = 0;
    static final int IDD_RESTART = 1;
    static final int IDD_HINT = 2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	addPreferencesFromResource(R.xml.settings);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	super.onCreateOptionsMenu(menu);

	this.myMenu = menu;
	myMenu.add(0, 1, 0, R.string.menu_about_save);
	return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case 1:
	    if (!YourSmsActivity.prefs.getString("lang", "default")
		    .equalsIgnoreCase(YourSmsActivity.lang)) {
		showDialog(IDD_RESTART);
	    } else {
		if (YourSmsActivity.prefs.getBoolean("do_not_show", false)) {
		    finish();
		} else {
		    showDialog(IDD_HINT);
		}
	    }
	    break;
	}
	return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_BACK) {
	    if ((YourSmsActivity.prefs.getBoolean("sms_love", false) != YourSmsActivity.sms_love)
		    || (YourSmsActivity.prefs.getBoolean("sms_skandal", false) != YourSmsActivity.sms_skandal)
		    || (YourSmsActivity.prefs.getBoolean("sms_congratulations",
			    false) != YourSmsActivity.sms_congratulations)
		    || (YourSmsActivity.prefs.getBoolean("sms_humor", false) != YourSmsActivity.sms_humor)
		    || (YourSmsActivity.prefs.getString("lang", "default") != YourSmsActivity.lang)) {
		showDialog(IDD_CONFIRM);
	    } else {
		finish();
	    }
	} else if (keyCode == KeyEvent.KEYCODE_MENU) {

	}
	return false;
    }

    @Override
    protected Dialog onCreateDialog(int id) {
	AlertDialog.Builder builder = new AlertDialog.Builder(this);
	switch (id) {
	case IDD_CONFIRM:
	    builder.setMessage(getString(R.string.dialog_confirm))
		    .setCancelable(false)
		    .setPositiveButton(getString(R.string.clear_table_yes),
			    new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,
					int id) {
				    setPrevValues();
				    dialog.cancel();
				    finish();
				}
			    })
		    .setNegativeButton(getString(R.string.clear_table_no),
			    new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,
					int id) {
				    dialog.cancel();
				}
			    });
	    return builder.create();

	case IDD_RESTART:
	    builder.setMessage(getString(R.string.dialog_restart))
		    .setCancelable(false)
		    .setPositiveButton(getString(R.string.clear_table_yes),
			    new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,
					int id) {
				    dialog.cancel();
				    System.exit(0);
				}
			    });
	    return builder.create();

	case IDD_HINT:
	    final Dialog dialog = new Dialog(Settings.this);
	    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
	    dialog.setContentView(R.layout.hint);
	    dialog.setCancelable(true);

	    Button button = (Button) dialog.findViewById(R.id.hintOk);
	    final CheckBox checkBox = (CheckBox) dialog
		    .findViewById(R.id.hintCheckBox);

	    button.setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
		    if (checkBox.isChecked()) {
			YourSmsActivity.prefs.edit().putBoolean("do_not_show",
				true).commit();
		    }

		    dialog.cancel();
		    Settings.this.finish();
		}
	    });

	    dialog.show();
	    return dialog;
	default:
	    return null;
	}
    }

    private void setPrevValues() {
	YourSmsActivity.prefs.edit()
		.putBoolean("sms_love", YourSmsActivity.sms_love).commit();
	YourSmsActivity.prefs.edit()
		.putBoolean("sms_skandal", YourSmsActivity.sms_skandal)
		.commit();
	YourSmsActivity.prefs
		.edit()
		.putBoolean("sms_congratulations",
			YourSmsActivity.sms_congratulations).commit();
	YourSmsActivity.prefs.edit()
		.putBoolean("sms_humor", YourSmsActivity.sms_humor).commit();
	YourSmsActivity.prefs.edit().putString("lang", YourSmsActivity.lang)
		.commit();
    }
}
